<?php if(!defined('BASEPATH')) exit('No direct script access allowed');

require APPPATH . '/libraries/BaseController.php';
/**
 * Class : User (UserController)
 * User Class to control all user related operations.
 * @author : Kishor Mali
 * @version : 1.1
 * @since : 15 November 2016
 */
class Upload extends BaseController
{
    /**
     * This is default constructor of the class
     */
    public function __construct()
    {
        parent::__construct();
        $this->load->model('user_model');
        $this->isLoggedIn();   
        $this->load->helper(array('form', 'url'));
    }

    public function index(){
        // $this->load->library("hei_excel");
        
        // $data = [
        //     'A1' => '公司名称', 'B1' => '项目名称', 'C1' => '立项时间', 'D1' => '项目地址', 'E1' => '技术负责人', 'F1' => '手机号',
        //     'A2' => 'company_name', 'B2' => 'project_name',
        //     'C2' => 'create_time', 'D2' => 'address',
        //     'E2' => 'tec_leader_name', 'F2' => 'tec_leader_phone',
        //     'A4' => '项目状态', 'B4' => '抽签时间', 'C4' => '预计抽检时间', 'D4' => '抽检人员', 'E4' => '职务', 'F4' => '手机号',
        //     'A5' => 'status_name', 'B5' => 'draw_date',
        //     'C5' => 'plan_samp_date', 'D5' => 'samp_user_name',
        //     'E5' => 'samp_user_job', 'F5' => 'samp_user_phone',
        //     'A7' => '分数', 'B7' => '检查时间',
        //     'A8' => 'score', 'B8' => 'samp_date',
        //     'A10' => '检查项', 'B10' => '检查楼层',
        // ];
        // $options = [
        //     'print' => true,//打印格式
        //     'alignCenter' => array_keys($data),//居中
        //     'setWidth' => ['A'=>30, 'B'=>30, 'C'=>30, 'D'=>30, 'E'=>30, 'F'=>30],
        //     'setARGB' => ['A1'],
        //     'bold' => [
        //         'A1', 'B1', 'C1', 'D1', 'E1', 'F1', 'A4', 'B4', 'C4', 'D4', 'E4', 'F4', 'A7', 'B7', 'C7', 'D7', 'E7', 'F7', 'A10', 'B10'
        //     ],//加粗
        // ];
        // // $this->hei_excel->exportExcel($data, '黑爷爷.xlsx', $options);
        $this->global['pageTitle'] = 'CodeInsect : Upload';
        $this->loadViews("upload/index", $this->global, array('error' => ' ' ) , NULL);

    }

    public function do_upload()
    {
        $config['upload_path']      = './uploads/';
        $config['allowed_types']    = 'gif|jpg|png|txt|xlsx|xls|csv';
        $config['max_size']     = 1024 * 10;
        $config['max_width']        = 1024 * 10;
        $config['max_height']       = 768 * 10;

        $this->load->library('upload', $config);
        $this->global['pageTitle'] = 'CodeInsect : Upload';

        if ( ! $this->upload->do_upload('userfile'))
        {
            $error = array('error' => $this->upload->display_errors(), 'upload_data' => $this->upload->data());

            $this->loadViews('upload/index', $this->global, $error);
        }
        else
        {
            $data = array('upload_data' => $this->upload->data());
            
            $this->loadViews('upload/upload_success', $this->global, $data);
        }
    }

    private function jsonArray($arr) {
        $a = '[';
        foreach ($arr as $key => $value) {
            $a = $a.'"'.$value.'"';
        }
        $a = $a.']';
        return $a;
    }

    public function compile_excel() {
        $this->load->library("hei_excel");
        $this->global['pageTitle'] = 'CodeInsect : Excel';
        $option = array(
            'mergeCells' => [],
            'formula' => [],
            'format'=> []
        );
        $fileName = 'test02.xls';
        $data = $this->hei_excel->importExcel('./uploads/'.$fileName, 0, 0, $option);

        $order = array(
            'details' => array()
        );
        foreach (array_values($data) as $key => $value) {
            if($key != 0 && $key != sizeof($data) - 1) {
                $value = array_values($value);
                array_push($order['details'], array(
                    'time_for_purchase' => $value[0],
                    'purchasing_agent'  => $value[1],
                    'time_for_delivery' => $value[2],
                    'spec_no'           => $value[3],
                    'goods_name'        => $value[4],
                    'asin'              => $value[5],
                    'quantity'          => $value[6],
                    'price'             => $value[7],
                    'total'             => $value[8],
                    'gift_box_size'     => $value[9],
                    'carton_size'       => $value[10],
                    'piece_weight'      => $value[11],
                    'package_weight'    => $value[12],
                    'weight'            => $value[13],
                    'amount_per_box'    => $value[14],
                    'amount_box'        => $value[15],
                    'remark'            => $value[16]
                ));
            }
                
        }
        $this->load->model('amazonOrder_model');
        // $this->amazonOrder_model->createOrder($order);
        // $this->amazonOrder_model->deleteOrder(1);
        // $res = $this->amazonOrder_model->getOrders(10, 0);
        // $res = $this->amazonOrder_model->getOrder(2);
        // echo "<pre>";
        // print_r($data);
        // return;
        
        $this->loadViews('upload/excel_view', $this->global, array(
            'data' => $data, 
            'mergeCells' => $this->jsonArray($option['mergeCells']),
            'file' => $fileName
        ));

    }

    
    
}

?>